import {getWy} from '@/uni_modules/wy-core/context' 
const state={
	wssConnected:false,
	wssFailTime:0,
	wssPingUnix:0,
	wssSendList:[],
	wssPongOpen:false,
	wssPongTimes:0,
	wssNoticeModel:{
		message:{
			count:0,
			list:[]
		}
	}
};
const getters={
	WssConnected:state=>{
		return state.wssConnected
	},
	MessageCount:state=>{
		let res=0
		let {message}=state.wssNoticeModel
		if(message){
			let {count}=message
			if(count){
				res=parseInt(count)
			}
		}
		return res
	}
};
const mutations={
	
};
const actions={
	setMessageCount({state}){
		let {message}=state.wssNoticeModel
		if(message){
			let {count}=message
			state.wssNoticeModel.message.count=0
		}
	},
	heartbit({dispatch,state}){
		state.wssPongTimes++
		if(state.wssConnected){
			dispatch('sendMessage',{
				type:'pong',
				content:'hi',
				times:state.wssPongTimes
			})
		}
		if(state.wssPongOpen){
			state.wssPongOpen=true
		}else{
			setTimeout(function(){
				dispatch('heartbit')
			},30000)
		}
		
	},
	startWebSocket({dispatch,state}){
		let wy=getWy()
	 	uni.connectSocket({
	 		url: wy.config.Sys.wss.host,
	 		data() {
	 			return {
					content:'hello'
	 			}
	 		},
	 		// #ifdef MP
	 		header: {
	 			'content-type': 'application/json'
	 		},
	 		// #endif
	 		// #ifdef MP-WEIXIN
	 		method: 'GET',
	 		// #endif
	 		success(res) {
	 			// 这里是接口调用成功的回调，不是连接成功的回调，请注意
	 		},
	 		fail(err) {
	 			// 这里是接口调用失败的回调，不是连接失败的回调，请注意
	 		}
	 	})
	 	uni.onSocketOpen((res) => {
	 		//this.connecting = false
	 		//this.connected = true
	 		/*uni.showToast({
	 			icon: 'none',
	 			title: '连接成功'
	 		})
			*/
		    console.log('SocketOpen')
			state.wssConnected=true
			/*
			setTimeout(function(){
				dispatch('sendList')
			},3000)
			*/
			setTimeout(function(){
				dispatch('heartbit')
			},10000)
			
	 	})
	 	uni.onSocketError((err) => {
	 		state.wssFailTime++
			if(state.wssFailTime>10){
				uni.showToast({
					icon: 'none',
					title: '连接失败，可能是websocket服务不可用!'
				})
			}
	 		else{
				console.log('onError', err);
				setTimeout(function(){
					dispatch('startWebSocket')
				},10000)
			}
	 	})
	 	uni.onSocketMessage((res) => {
			let {data}=res
			if(data){
				data=JSON.parse(data)
				if(data){
					//console.log(data)
					let {type}=data
					switch(type){
						case 'ping':
							state.wssPingUnix=unix()
							break
						case 'notice':
							let {notice}=data
							if(notice&&isObject(notice)){
								state.wssNoticeModel={
									...state.wssNoticeModel,
									...notice
								}
							}
							break
					}
				}
				
			}
	 		//this.msg = res.data
	 		//console.log('onMessage', res)
	 	})
	 	uni.onSocketClose((res) => {
			state.wssConnected=false
	 		//console.log('onClose', res)
	 	}) 
	},
	loginWebSocket({dispatch},option){
		let {sid}=option
		let data={
			type:'login',
			client_name:sid,
			room_id:App.sitekey//Sys.wss.room_id
		}
		dispatch('sendMessage',data)
	},
	sendList({state,dispatch}){
		if(state.wssConnected){
			let list=state.wssSendList
			state.wssSendList=[]
			for(let i=0;i<list.length;i++){
				dispatch('sendMessage',list[i])
			}
		}
	},
	sendMessage({state},data) {
		if(state.wssConnected){
			uni.sendSocketMessage({
				data: JSON.stringify(data),
				success(res) {
//					console.log(res);
				},
				fail(err) {
					console.log(err);
				}
			})
		}else{
			state.wssSendList.push(data)
		}
		
	},
	
};
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}